/**
 * Copyright 厦门中软海晟信息技术有限公司 版权所有 违者必究 2019
 */
package com.vijos;

import java.util.HashMap;

/**
 *
 *@author : sunzb(sunzb@hsit.com.cn)
 *@date: 2019/4/17
 */
//字符串还原
public class P2_2_1449 {

    public static String test(String s1, String s2, String s3) {
        char[] s1Arr = s1.toCharArray();
        char[] s2Arr = s2.toCharArray();
        char[] s3Arr = s3.toCharArray();
        if (is(s1Arr, s2Arr, s3Arr)) {
            return new StringBuilder().append(s1Arr).reverse().toString();
        }
        if (is(s2Arr, s1Arr, s3Arr)) {
            return new StringBuilder().append(s2Arr).reverse().toString();
        }
        if (is(s3Arr, s1Arr, s3Arr)) {
            return new StringBuilder().append(s3Arr).reverse().toString();
        }
        throw new RuntimeException("无效的密码");
    }

    private static boolean is(char[] s1Arr, char[] s2Arr, char[] s3Arr) {
        char[] newChars = new char[s1Arr.length];
        for (int i = s1Arr.length - 1; i >= 0; --i) {
            newChars[s1Arr.length - 1 - i] = s1Arr[i];
        }
        for (int i = 0; i < s1Arr.length; i++) {
            int value = (int) newChars[i];
            int v2 = (int) s2Arr[i];
            int v3 = (int) s3Arr[i];
            int deltaLeft;
            int deltaRight;
            if (v2 > v3) {
                deltaLeft = value - v3;
                deltaRight = v2 - value;
            } else {
                deltaLeft = value - v2;
                deltaRight = v3 - value;
            }
            if (deltaLeft != deltaRight) {
                if (deltaLeft < 0) {
                    if (26 + deltaLeft != deltaRight) {
                        return false;
                    }
                }
                if (deltaRight < 0) {
                    if (deltaLeft != 26 + deltaRight) {
                        return false;
                    }
                }
                // 无效的密码，距离不相等
                if (deltaLeft > 0 && deltaRight > 0) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        /*char c = 'a';
        System.out.println((int) c);
        c = 'z';
        System.out.println((int) c);*/
        System.out.println(test("zabc", "dcba", "bcde"));
        System.out.println(test("ghij", "kjih", "ijkl"));
        System.out.println(test("ghij", "kjih", "ijll"));
    }
}
